<?php


namespace app\month\server;


use think\Exception;
use think\facade\Log;

class Baidu
{



    /**
     * 审核内容
     */
    public static function auditingContent($text){
        $token = self::getAccessToken();
        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=' . $token;
        $bodys = array(
            'text' => $text
        );
        $res = request_post($url, $bodys);
        if (isset($res['conclusionType']) && $res['conclusionType'] == 1) {
            return true;
        }
        throw new Exception("内容不合法、请重新填写",3101);
    }

    /**
     * 获取accessToken
     */
    public static function getAccessToken(){
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $config = config('third.baidu');
        $post_data['grant_type']       = $config['grant_type'];
        $post_data['client_id']      = $config['client_id'];
        $post_data['client_secret'] = $config['client_secret'];
        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $res = request_post($url, $post_data);
        if (isset($res['error'])) {
            Log::error(json_encode($res));
           throw new Exception("第三异常、请稍后重试",1010);
        }

        return $res['access_token'];

    }
}